package top.skihub.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.dao.DataAccessException;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import top.skihub.pojo.Result;
import top.skihub.pojo.ShoppingTrolley;
import top.skihub.service.ShoppingTrolleyService;

import java.util.List;

@Slf4j
@RestController
@CrossOrigin // 添加跨域支持
public class ShoppingTrolleyController {

    @Autowired
    private ShoppingTrolleyService shoppingTrolleyService;

    @Value("${server.port}")
    private String port;

    @PostMapping("/addShoppingTrolley/{goodsId}/{userId}")
    public ResponseEntity<?> addGoods(@PathVariable String goodsId,
                                    @PathVariable String userId) {
        try {
            // 参数验证
            Integer goodsIdInt;
            Integer userIdInt;
            
            try {
                goodsIdInt = Integer.parseInt(goodsId);
                userIdInt = Integer.parseInt(userId);
            } catch (NumberFormatException e) {
                log.error("参数转换失败: goodsId={}, userId={}", goodsId, userId);
                return ResponseEntity.badRequest()
                    .body(Result.error("无效的参数格式"));
            }

            if (goodsIdInt <= 0 || userIdInt <= 0) {
                log.error("无效的参数值: goodsId={}, userId={}", goodsIdInt, userIdInt);
                return ResponseEntity.badRequest()
                    .body(Result.error("参数值必须大于0"));
            }

            log.info("服务端口: {}, 添加商品到购物车: goodsId={}, userId={}", port, goodsIdInt, userIdInt);
            shoppingTrolleyService.addShoppingTrolley(goodsIdInt, userIdInt);
            return ResponseEntity.ok(Result.success());
            
        } catch (IllegalStateException e) {
            log.warn("服务端口: {}, 商品已在购物车中: {}", port, e.getMessage());
            return ResponseEntity.ok(Result.error(e.getMessage()));
            
        } catch (IllegalArgumentException e) {
            log.error("参数错误: {}", e.getMessage());
            return ResponseEntity.badRequest()
                .body(Result.error(e.getMessage()));
            
        } catch (DataAccessException e) {
            log.error("数据库访问异常", e);
            return ResponseEntity.internalServerError()
                .body(Result.error("系统错误"));
            
        } catch (Exception e) {
            log.error("添加购物车异常", e);
            return ResponseEntity.internalServerError()
                .body(Result.error("系统异常"));
        }
    }

    @GetMapping("/getShoppingTrolley/{userId}")
    public Result list(@PathVariable String userId) {
        try {
            Integer userIdInt;
            try {
                userIdInt = Integer.parseInt(userId);
            } catch (NumberFormatException e) {
                log.error("参数转换失败: userId={}", userId);
                return Result.error("无效的用户ID格式");
            }

            if (userIdInt <= 0) {
                log.error("无效的用户ID: userId={}", userIdInt);
                return Result.error("用户ID必须大于0");
            }

            log.info("服务端口: {}, 获取购物车列表: userId={}", port, userIdInt);
            List<ShoppingTrolley> shoppingTrolley = shoppingTrolleyService.getShoppingTrolley(userIdInt);
            return Result.success(shoppingTrolley);
        } catch (Exception e) {
            log.error("获取购物车列表异常", e);
            return Result.error("获取购物车列表失败");
        }
    }

    @DeleteMapping("/deleteShoppingTrolley/{goodsId}/{userId}")
    public Result delete(@PathVariable String goodsId, @PathVariable String userId) {
        try {
            Integer goodsIdInt;
            Integer userIdInt;
            try {
                goodsIdInt = Integer.parseInt(goodsId);
                userIdInt = Integer.parseInt(userId);
            } catch (NumberFormatException e) {
                log.error("参数转换失败: goodsId={}, userId={}", goodsId, userId);
                return Result.error("无效的参数格式");
            }

            if (goodsIdInt <= 0 || userIdInt <= 0) {
                log.error("无效的参数值: goodsId={}, userId={}", goodsIdInt, userIdInt);
                return Result.error("参数值必须大于0");
            }

            log.info("服务端口: {}, 删除购物车商品: goodsId={}, userId={}", port, goodsIdInt, userIdInt);
            shoppingTrolleyService.deleteShoppingTrolley(goodsIdInt, userIdInt);
            return Result.success();
        } catch (IllegalArgumentException e) {
            log.error("参数错误: goodsId={}, userId={}", goodsId, userId, e);
            return Result.error(e.getMessage());
        } catch (Exception e) {
            log.error("删除购物车商品异常: goodsId={}, userId={}", goodsId, userId, e);
            return Result.error("删除购物车商品失败");
        }
    }
}
